package com.xuetang9.t9.hotel.utils;

import java.io.Serializable;

public class ResponseResult<T>  implements Serializable {

    private T data;

    private Integer code;

    private String errorMessage;

    public ResponseResult() {
        this.code = 200;
    }

    public ResponseResult(T data, Integer code, String errorMessage) {
        this.data = data;
        this.code = code;
        this.errorMessage = errorMessage;
    }

    public ResponseResult(Integer code, String errorMessage) {
        this.code = code;
        this.errorMessage = errorMessage;
    }

    public ResponseResult(T data, Integer code) {
        this.data = data;
        this.code = code;
    }


    public ResponseResult(T data, String errorMessage) {
        this.data = data;
        this.errorMessage = errorMessage;
    }


    public static ResponseResult okResult(int code, String msg){
        ResponseResult responseResult = new ResponseResult();
        return responseResult.ok(code,msg,null);
    }



    public static ResponseResult okResult(Object data){
        ResponseResult responseResult = setHttpCodeEnum(HttpCodeEnum.SUCCESS,HttpCodeEnum.SUCCESS.getErrorMessage());
        if(null != data){
            responseResult.setData(data);
        }
        return responseResult;
    }

    private static ResponseResult setHttpCodeEnum(HttpCodeEnum httpCodeEnum, String errorMessage){
        return okResult(httpCodeEnum.getCode(),httpCodeEnum.getErrorMessage());
    }

    public ResponseResult<?> ok(Integer code, T data){
        this.code = code;
        this.data = data;
        return this;
    }
    public ResponseResult<?> ok(T data){
        this.data = data;
        return this;
    }
    public ResponseResult<?> ok(Integer code, String errorMessage,T data){
        this.code = code;
        this.errorMessage = errorMessage;
        this.data = data;
        return this;
    }

    public ResponseResult<?> error(Integer code, String msg){
        this.code = code;
        this.errorMessage = msg;
        return this;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    @Override
    public String toString() {
        return "ResponseResult{" +
                "data=" + data +
                ", code=" + code +
                ", errorMessage='" + errorMessage + '\'' +
                '}';
    }
}
